package zrweb;

import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EnemyPosition
{
	private Connection connect = null;
	private String message = "";
	
	public void getEnemyPos(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		// get the username and password
		String username = request.getParameter("username");
		
		// connect to database to update wetherOnService column and clear locations
		ConnDB conndb = new ConnDB();
		
		// NOTE: string -> varchar for posrgresql query by using ''
		String selectSQL = "select username, reportlat, reportlon from userinfor where onservice = TRUE and" + 
						   " username !='" + username + "'";
		// give feedback on whether can select or not for Android mobile
		DataOutputStream dos = new DataOutputStream(response.getOutputStream());
		try
		{
			connect = conndb.getConn();
			Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stmt.executeQuery(selectSQL);
			while (rs.next())
			{
				message = message + rs.getString("reportlat")+" "+rs.getString("reportlon") + " " + rs.getString("username") + " ";
			}
			// pass the query result to the mobile side
			dos.writeUTF("Succeed " + message);
			rs.close();  
            stmt.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnDB.releaseConnection(connect);
		}
	}
}
